﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using WyUnity.Data.Query;
using WyUnity.Data.TableMapper;

namespace RFID_Interface.DTO
{
    public class DeliveryBillResponse : ViewBase<DeliveryBillResponse>
    {

        public char Result { get; set; }
        public string ErrMsg { get; set; }

        [Map]
        public int ID { get; set; }
        [Map]
        public string BillNo { get; set; }
        [Map]
        public string Remark { get; set; }
        [Map]
        public DateTime Date { get; set; }
        [Map]
        public string Biller { get; set; }
        [Map]
        public int Status { get; set; }
        [Map]
        public string SendStockNum { get; set; }
        [Map]
        public int BillType { get; set; }
        [Map]
        public string CustomerNumber { get; set; }
        [Map]
        public string CustomerName { get; set; }
        [Map]
        public string OrderBillNo { get; set; }
        [Map]
        public string Receiver { get; set; }
        [Map]
        public string Address { get; set; }
        [Map]
        public string ReceiverTel { get; set; }
        [Map]
        public int ROB { get; set; }

        public List<DeliveryBillEntry> Entry { get; set; }

        public override string GetSQLClip()
        {
            return @"
SELECT  isb.FBillNo BillNo ,
        isb.FInterID ID ,
        isb.FTranType BillType ,
        isb.FNote Remark ,
        isb.FDate [Date] ,
        tu.FName Biller ,
        isb.FStatus [Status] ,
        ts.FNumber SendStockNum,
        [to].FNumber CustomerNumber,
        [to].FName CustomerName,
        so.FBillNo OrderBillNo,
        so.FHeadSelfS0133 Receiver,
        so.FHeadSelfS0141 [Address],
        so.FHeadSelfS0140 ReceiverTel,
        isb.FROB ROB
FROM    ICStockBill_1001 AS isb
        LEFT JOIN SEOrder AS so ON so.FInterID=isb.FOrderInterID
        LEFT JOIN t_Stock AS ts ON so.FHeadSelfS0132 = ts.FItemID
        LEFT JOIN t_Organization AS [to] ON so.FCustID=[to].FItemID
        LEFT JOIN t_User AS tu ON isb.FBillerID = tu.FUserID
";
        }

        public static FieldBase pBillNo = new Field<DeliveryBillResponse>(p => p.BillNo);

        public static DeliveryBillResponse Get(string billno)
        {
            using (var db = DB.Open())
            {
                var rlt = db.QuerySingle<DeliveryBillResponse>(DeliveryBillResponse.pBillNo == billno);

                if (rlt == null)
                {
                    rlt = new DeliveryBillResponse();
                    rlt.Result = 'F';
                    rlt.ErrMsg = "单据不存在:" + billno;
                    return rlt;
                }

                rlt.Entry = db.QueryList<DeliveryBillEntry>(DeliveryBillEntry.pID == rlt.ID);

                rlt.ErrMsg = string.Empty;
                rlt.Result = 'S';
                return rlt;
            }
        }

    }
    public class DeliveryBillEntry : ViewBase<DeliveryBillEntry>
    {
        [Map]
        public int ID { get; set; }
        [Map]
        public int LineID { get; set; }
        [Map]
        public string ICItemNumber { get; set; }
        [Map]
        public string ICItemName { get; set; }
        [Map]
        public decimal Qty { get; set; }
        [Map]
        public string Unit { get; set; }
        [Map]
        public string PlanDate { get; set; }

        public static FieldBase pID = new Field<DeliveryBillEntry>(p => p.ID);

        public override string GetSQLClip()
        {
            return @"SELECT
isbe.FInterID ID,
isbe.FEntryID LineID,
tii.FNumber ICItemNumber,
tii.FName ICItemName,
isbe.FQty Qty,
tmu.FName Unit,
'' PlanDate
FROM ICStockBillEntry_1001 AS isbe
INNER JOIN t_ICItem AS tii ON isbe.FItemID = tii.FItemID
INNER JOIN t_MeasureUnit AS tmu ON tii.FUnitID=tmu.FItemID";
        }
    }
}